/*zhaoyihang
 * 注册
 * */
#include "threadPool.h"

int Register(int netfd,MYSQL *mysql)
{
    while(1)
    {   
        train_t train;
        int length;
        char username[1024]={0};
        //接收用户名
        recvn(netfd,&length,sizeof(length));
        recvn(netfd,username,length);
        printf("客户端发来的用户名为%s\n",username);

        //检查名字是否重复
        char bufForCheckName[1024];
        snprintf(bufForCheckName, sizeof(bufForCheckName), "SELECT * FROM UserTable WHERE username='%s'", username);
        int qret = mysql_query(mysql,bufForCheckName);

        printf("返回的qret=%d\n",qret);
        //是否查到数据库
        if(qret !=0)
        {
            printf("Failed to execute query: %s\n", mysql_error(mysql));
            mysql_close(mysql);
            return 1;
        }
        // 查询成功，获取查询结果
        MYSQL_RES *result = mysql_store_result(mysql);
        if (result == NULL) 
        {
            printf("Failed to get query result: %s\n", mysql_error(mysql));
            mysql_close(mysql);
            return 1;
        }
        // 检查查询结果
        int num_rows = mysql_num_rows(result);
        if (num_rows == 0) 
        {
            printf("开始注册！\n"); 
            //发送盐值
            bzero(&train,sizeof(train));
            train.length = strlen("ZLCXWY007"); 
            memcpy(train.data,"ZLCXWY007",strlen("ZLCXWY007"));
            send(netfd, &train, 4+train.length, MSG_WAITALL);
            //接受客户端的密文
            int buflen;
            char encryptedPass[4096];
            recv(netfd, &buflen, sizeof(int), MSG_WAITALL);
            recv(netfd, encryptedPass, buflen, MSG_WAITALL);

            //将用户名和密文上传到数据库
            char buf1[1024];
            snprintf(buf1, sizeof(buf1),
                     "INSERT INTO UserTable (username, salt, encrypted_pass, tomb) VALUES ('%s', 'ZLCXWY07', '%s', 0)",
                     username,encryptedPass);
            int qret2 = mysql_query(mysql,buf1);
            if(qret2 !=0)
            {
                fprintf(stderr,"sql error :%s\n",mysql_error(mysql));
                bzero(&train,sizeof(train));
                train.length = strlen("ZLCXWY007"); 
                memcpy(train.data,"ZLCXWY007",strlen("ZLCXWY007"));
            }
            else
            {
                printf("注册成功！\n");
                bzero(&train,sizeof(train));
                train.length = strlen("y"); 
                memcpy(train.data,"y",strlen("y"));
                send(netfd, &train, 4+train.length, MSG_WAITALL);

                //todo
                char path[256] = "storage/";
                char *path_dir = strcat(path,username);
                if (mkdir(path_dir, 0666) == -1) 
                {
                    perror("mkdir() failed");
                    return 1;
                }

                //将用户根目录上传到数据库
                char buf_path[1024];
                snprintf(buf_path, sizeof(buf_path),
                         "INSERT INTO FileTable (filename, username, pre_id, type, path, md5,tomb) VALUES ('%s', '%s', -1, 'dir','%s',NULL,0)",
                         username,username,path_dir);
                int qret_path = mysql_query(mysql,buf_path);
                printf("qret_path = %d\n",qret_path);
                
                
                if(qret_path !=0)
                {
                    fprintf(stderr,"sql error :%s\n",mysql_error(mysql));
                    printf("灵异事件\n");
                }
                else
                {
                    printf("用户根目录上传数据库成功");
                }
            }

        }
        else
        {
            printf("用户已存在\n");
        }
    }

    return 0;
}
